layui.config({
	base : "js/"
}).use(['form','layer','jquery','layedit','upload','laydate'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		layedit = layui.layedit,
		laydate = layui.laydate,
		$ = layui.jquery;

	var orgId = window.sessionStorage.getItem("organizeEditId");
	
 	var editIndex = layedit.build('news_content');
 	form.on("submit(subInfo)",function(data){
   		layedit.sync(editIndex);
		var data={
			name:$(".name").val(),
 			officeSite:$(".officeSite").val(),
 			officeTel:$(".officeTel").val(),
 			tenureTime:$(".tenureTime").val(),
 			introduce:$("#news_content").val(),
 			orgId:orgId
 		}
   		var index = layer.load(1, {shade: [0.1,'#fff'] });//0.1透明度的白色背景
   		updateInfo(data,function(){
   			layer.close(index);
   		});
 		return false;
 	})
 	layui.upload({
    	elem: '.memLoad', //绑定元素
    	url : $.httpRootPath() + "/api/web/organize/i/"+orgId+"/picture",
    	type: 'images',
		success: function(res){
         	if(res.code != "00"){
         		layer.msg("服务器异常，请稍候再试");
         	}else{
         		layer.msg("修改成功");
         		$("#orgLogo").attr("src",res.data.picture);
         	}
  		},
         error: function(){
         	 console.log("失败"); 
      	//请求异常回调
    	}
    });
 	
 	form.verify({
  		tel: function(value, item){ //value：表单的值、item：表单的DOM对象
    	if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
    	  return '用户名不能有特殊字符';
   		}
   	 	if(/(^\_)|(\__)|(\_+$)/.test(value)){
    	  return '用户名首尾不能出现下划线\'_\'';
   		}
    	if(/^\d+\d+\d$/.test(value)){
      		return '用户名不能全为数字';
    	}
 	 },
  //我们既支持上述函数式的方式，也支持下述数组的形式
  //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
  	pass: [
   	 /^[\S]{6,12}$/,'密码必须6到12位，且不能出现空格'
  	] 
	});  
 	//加载默认数据
 	
	var data ={
		orgId:orgId
	}
	getInfo(data,function(){
		console.log('ORGANIZE:['+data.orgId+']的数据加载完成');
	})
 	//填充数据方法
 	function fillData(data){
 		$(".name").val(data.name);      
		$(".mark").val(data.mark);        
		$(".tenureTime").val(data.tenureTime);    
		$(".updateTime").val(data.updateTime);        
		$(".officeSite").val(data.officeSite);    
		$(".officeTel").val(data.officeTel); 
		$(".founder").val(data.founder); 
		$("#news_content").val(data.introduce);	
		if(data.picture != ""){
			$("#orgLogo").attr("src",data.picture);
		}
		$('[name="otype"]').attr("disabled", "disabled");
		$("input[name=otype][value=" + data.type + "]").attr("checked", true); //设置当前性别选中项
		form.render('radio');
 	}
 	function getInfo(data, callback) {
	$.ajax({
		url: $.httpRootPath() + "/api/web/organize/i/" + orgId,
		type: "get",
		dataType: "json",
		success: function(data) {
			if(data.code == "00") {
				fillData(data.data);
			} else {
				layer.alert('加载失败', {icon: 6,title: '友情提示'});
			}
		},
		complete: function() {
			callback();
		}
	})

	}
 	function updateInfo(data, callback) {
	$.ajax({
		url: $.httpRootPath() + '/api/web/organize/i/' + data.orgId,
		type: 'PUT',
		dataType: 'json',
		data: data,
		timeout: 15000,
		cache: false,
		success: function(r) {
			if(r.code == "00") {
				layer.msg("更新成功！");
			} else {
				layer.msg("更新失败！");
			}
		},
		error: function(e) {
			layer.msg("系统错误！");
		},
		complete: function() {
			callback();
		}
	});
	}
})
